<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\ApplicationProcesses;
use Illuminate\Http\Request;

class ApproveProcessesController extends Controller
{
    public function index(Request $Request) {
        $type = $Request->type;
        $data = ApplicationProcesses::when($type, function ($query, $type) {
            $query->where('type',$type);
        })->with('role')->with('user')->where('project_id',$Request->project_id)->orderBy('level','asc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function store(Request $Request)
    {
        if(!$Request->role_id){
            // return response()->json(['code' => '404', 'msg' => '请选择岗位']);
        }
        if(!$Request->user_id){
            return response()->json(['code' => '404', 'msg' => '请选择用户']);
        }
        if(!$Request->level){
            return response()->json(['code' => '404', 'msg' => '请输入层级']);
        }
        if(!$Request->type){
            // return response()->json(['code' => '404', 'msg' => '请选择类型']);
        }
        $ApplicationProcesses = ApplicationProcesses::where('user_id',$Request->user_id)->where('project_id',$Request->project_id)->first();
        if(!empty($ApplicationProcesses)){
            // return response()->json(['code' => '500', 'msg' => '该人员已存在']);
        }
        $data = new ApplicationProcesses();
        $data->role_id = $Request->role_id ?? 0;
        $data->project_id = $Request->project_id;
        $data->user_id = $Request->user_id;
        $data->level = $Request->level;
        $data->type = $Request->type ?? '';
        $data->save();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }

    public function update(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '参数错误']);
        }
        if(!$Request->role_id){
            // return response()->json(['code' => '404', 'msg' => '请选择岗位']);
        }
        if(!$Request->user_id){
            return response()->json(['code' => '404', 'msg' => '请选择用户']);
        }
        if(!$Request->level){
            return response()->json(['code' => '404', 'msg' => '请输入层级']);
        }
        if(!$Request->type){
            // return response()->json(['code' => '404', 'msg' => '请选择类型']);
        }
        $data = ApplicationProcesses::find($Request->id);
        $data->role_id = $Request->role_id ?? 0;
        $data->user_id = $Request->user_id;
        $data->level = $Request->level;
        $data->save();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function delete(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = ApplicationProcesses::find($Request->id);
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }
}
